import json
from urllib.parse import urlencode

import requests
import time


def ali_real_name_id(user_name, user_card):
    '''
    用户名和身份证号连网查询
    :param name: 姓名
    :param id_card: 身份证号
    :return: 状态码，校验后的信息（0000为校验成功）
    '''
    import urllib, urllib.request, sys
    import ssl

    host = 'https://idenauthen.market.alicloudapi.com'
    path = '/idenAuthentication'
    method = 'POST'
    appcode = '4b9df64ae1f54c598f9ba6f8d9a4accf'
    querys = ''
    bodys = {}
    url = host + path

    bodys['idNo'] = user_card
    bodys['name'] = user_name
    post_data = urlencode(bodys).encode(encoding='UTF8')
    request = urllib.request.Request(url, post_data)
    request.add_header('Authorization', 'APPCODE ' + appcode)
    # //根据API的要求，定义相对应的Content-Type
    request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    response = urllib.request.urlopen(request, context=ctx)
    content = response.read()
    print(content)
    content_dict = json.loads(content.decode())
    code = content_dict.get('respCode')
    respMessage = content_dict.get('respMessage')
    return code, respMessage